clear 
//Load Data for gas use analysis
if("`c(os)'"=="Windows"){
	local dataFile = "CleanedData\FHWACleanV4.csv"
}
else{
	local dataFile = "CleanedData/FHWACleanV4.csv"
}

import delimited using "`dataFile'"

//Add State Codes to all states
sort state
egen tempStateCode = mode(statecode), by(state)
replace statecode = tempStateCode
drop tempStateCode

//Label Variables
label var logregistrations "Log Total Registrations"
label var logvmt "Log VMT"
label var loglicenseddrivers "Log Licensed Drivers" 
label var logjointpopulation "Log Total Population" 
label var logrealmeangasprice "Log Mean Gas Price" 
label var logemployment  "Log Employment"
label var logrealjointincome  "Log Total Income"
label var logmetropop  "Log (1 + Metro Population)"
label var lognonmetropop  "Log (1 + Non-Metro Population)"
label var logmetrorealinc  "Log (1 + Metro Income)"
label var lognonmetrorealinc  "Log (1 + Non-Metro Income)"
label var logrealstategdp  "Log State GDP"
label var logpopulation  "Log Population"
label var logrealtotalincome  "Log Total Income"
label var logroadmileage "Log Road Mileage"
label var nosafetyind "Treatment"


//Create Inverse Hyperbolic Sine Variables 
gen asinhMetroInc = asinh(realmetroincome2018m)
gen asinhNonMetroInc = asinh(realnonmetroincome2018m)

gen asinhMetroPop = asinh(metropopulation)
gen asinhNonMetroPop = asinh(nonmetropopulation)


//Label Asinh variables
label var asinhMetroInc "Asinh(Metro Income)"
label var asinhNonMetroInc "Asinh(Non-Metro Income)"

label var asinhMetroPop "Asinh(Metro Population)"
label var asinhNonMetroPop "Asinh(Non-Metro Population)"

//per capita models
//construct outcome variables 
gen logRegPerCapita = logregistrations-log(metropopulation + nonmetropopulation)
gen logGasUsePerCapita = loghighwaygasuse-log(metropopulation + nonmetropopulation)


//Label treatment variables

label define nosafetyind 0 "No Treatment" 1 "Treatment"
label values nosafetyind nosafetyind


//Generate State Dummies
egen stateGroup = group(state)

//Drop Datapoints that are replications of past data for registrations
drop if statecode=="CO" & year==2006
drop if statecode=="IN" & (year==2006 | year==2007 | year==2009)
drop if statecode=="MT" & year==2005
drop if statecode=="NJ" & year==2008
drop if statecode=="PR" & (year==2002 | year==2003 | year==2005 | year==2008 | year==2009 | year==2010)
drop if statecode=="TX" & year==2009
drop if statecode=="IL" & year==2011
drop if statecode=="NH" & year==2012
drop if statecode=="NY" & year==2012

//Drop Datapoints that are replications of past data for gas use
drop if statecode=="RI" & (year==2003 | year==2005)


//Drop if data is prior to 1970
drop if year<1970

//Normalize differenece of outcome variables for identification of extreme points

sort stateGroup year
xtset stateGroup year

gen DiffLogReg = d.logregistrations
gen DiffLogHighwayGasUse = d.loghighwaygasuse
gen DiffLogRegPerCapita = d.logRegPerCapita
gen DiffLogGasUsePerCapita = d.logGasUsePerCapita

local normVarList = "DiffLogReg DiffLogHighwayGasUse DiffLogRegPerCapita DiffLogGasUsePerCapita"

sort stateGroup

foreach varToNorm in `normVarList'{
	
	by stateGroup: egen tempMean = mean(`varToNorm')
	by stateGroup: egen tempSD = sd(`varToNorm')
	by stateGroup: egen tempNumData = count(`varToNorm')
	gen tempSE = tempSD/sqrt(tempNumData)
	
	gen norm`varToNorm' = (`varToNorm'-tempMean)/tempSD
	
	drop tempMean tempSD tempNumData tempSE
	
	di "`varToNorm'"
	
}

//Generate Colorado Dummy
gen coDum1 = 0
gen coDum2 = 0
replace coDum1 = 1 if statecode=="CO" & year>=2002 & year<=2009
replace coDum2 = 1 if statecode=="CO" & year>=2010

drop if coDum1 == 1


//Generate dummy variable for data footnotes
gen transactionDataDummy = 0

replace transactionDataDummy = 1 if statecode=="AR" & year>=2011
replace transactionDataDummy = 2 if statecode=="GA" & year>=2011
replace transactionDataDummy = 3 if statecode=="IA" & year>=2011
replace transactionDataDummy = 4 if statecode=="IL" & year>=2012
replace transactionDataDummy = 5 if statecode=="KY" & year>=2011
replace transactionDataDummy = 6 if statecode=="LA" & year>=2012
replace transactionDataDummy = 7 if statecode=="ME" & year>=2011 //Maine is a state with what looks to not have a consistent change
replace transactionDataDummy = 8 if statecode=="ME" & year>2014
replace transactionDataDummy = 9 if statecode=="MI" & year>=2011 //Michigan is a state with what looks to not have a consistent change
replace transactionDataDummy = 10 if statecode=="MI" & year>2012
replace transactionDataDummy = 11 if statecode=="MN" & year>=2011
replace transactionDataDummy = 12 if statecode=="NV" & year>=2011
replace transactionDataDummy = 13 if statecode=="OK" & year>=2011
replace transactionDataDummy = 14 if statecode=="SD" & year>=2011
replace transactionDataDummy = 15 if statecode=="TN" & year>=2011
replace transactionDataDummy = 16 if statecode=="TX" & year>=2011
replace transactionDataDummy = 17 if statecode=="WA" & year>=2011
replace transactionDataDummy = 18 if statecode=="WI" & year>=2011
replace transactionDataDummy = 19 if statecode=="WY" & year>=2011




//Local Data Source Controls

sort state year

python

import numpy as np
import pandas as p 
from sfi import Data 


def getDSCDummies(data, col, cutoff):

	tempExtState = None
	
	tempIndex = 0
	stateCol = 0
	
	rows, cols = data.shape
	
	dummyExt = []
	
	for i in range(rows):
		tempDiff = float(data[i,col])
		tempState = data[i,stateCol]
		
		
		if(abs(tempDiff)>cutoff):
			tempExtState = tempState
			tempIndex+=1
			
		if tempState==tempExtState:
			dummyExt.append(tempIndex)
		else:
			dummyExt.append(0)
		
	
	dummyExt = np.array(dummyExt).reshape((-1,1))
	return dummyExt

dataCols = ['state', 'year', 'normDiffLogReg', 'normDiffLogHighwayGasUse', 'normDiffLogRegPerCapita', 'normDiffLogGasUsePerCapita']

data = np.array(Data.get(dataCols, missingval=np.nan))


regCutoffLow = 2.93 #Spec. 4
regCutoffPrimary = 4.02 #Spec. 3
regCutoffHigh = 7 #Spec. 2

gasUseCutoffLow = 2.93 #Spec. 4
gasUseCutoffPrimary = 4.02 #Spec. 3
gasUseCutoffHigh = 7 #Spec. 2

regCol = dataCols.index('normDiffLogReg')
gasUseCol = dataCols.index('normDiffLogHighwayGasUse')
regPerCapitaCol = dataCols.index('normDiffLogRegPerCapita')
gasUsePerCapitaCol = dataCols.index('normDiffLogGasUsePerCapita')

rows, cols = data.shape

regDummyExtLow = getDSCDummies(data, regCol, regCutoffLow)
regDummyExtPrimary = getDSCDummies(data, regCol, regCutoffPrimary)
regDummyExtHigh = getDSCDummies(data, regCol, regCutoffHigh)

gasUseDummyExtLow = getDSCDummies(data, gasUseCol, gasUseCutoffLow)
gasUseDummyExtPrimary = getDSCDummies(data, gasUseCol, gasUseCutoffPrimary)
gasUseDummyExtHigh = getDSCDummies(data, gasUseCol, gasUseCutoffHigh)

regPerCapitaDummyExtLow = getDSCDummies(data, regPerCapitaCol, regCutoffLow)
regPerCapitaDummyExtPrimary = getDSCDummies(data, regPerCapitaCol, regCutoffPrimary)
regPerCapitaDummyExtHigh = getDSCDummies(data, regPerCapitaCol, regCutoffHigh)

gasUsePerCapitaDummyExtLow = getDSCDummies(data, gasUsePerCapitaCol, gasUseCutoffLow)
gasUsePerCapitaDummyExtPrimary = getDSCDummies(data, gasUsePerCapitaCol, gasUseCutoffPrimary)
gasUsePerCapitaDummyExtHigh = getDSCDummies(data, gasUsePerCapitaCol, gasUseCutoffHigh)

Data.addVarInt('regDummyExtSpec4')
Data.store("regDummyExtSpec4",None, regDummyExtLow[:])
Data.addVarInt('regDummyExtSpec3')
Data.store("regDummyExtSpec3",None, regDummyExtPrimary[:])
Data.addVarInt('regDummyExtSpec2')
Data.store("regDummyExtSpec2",None, regDummyExtHigh[:])

Data.addVarInt('gasUseDummyExtSpec4')
Data.store("gasUseDummyExtSpec4",None, gasUseDummyExtLow[:])
Data.addVarInt('gasUseDummyExtSpec3')
Data.store("gasUseDummyExtSpec3",None, gasUseDummyExtPrimary[:])
Data.addVarInt('gasUseDummyExtSpec2')
Data.store("gasUseDummyExtSpec2",None, gasUseDummyExtHigh[:])

Data.addVarInt('regPerCapitaDummyExtSpec4')
Data.store("regPerCapitaDummyExtSpec4",None, regPerCapitaDummyExtLow[:])
Data.addVarInt('regPerCapitaDummyExtSpec3')
Data.store("regPerCapitaDummyExtSpec3",None, regPerCapitaDummyExtPrimary[:])
Data.addVarInt('regPerCapitaDummyExtSpec2')
Data.store("regPerCapitaDummyExtSpec2",None, regPerCapitaDummyExtHigh[:])

Data.addVarInt('gasPerCapitaDummyExtSpec4')
Data.store("gasPerCapitaDummyExtSpec4",None, gasUsePerCapitaDummyExtLow[:])
Data.addVarInt('gasPerCapitaDummyExtSpec3')
Data.store("gasPerCapitaDummyExtSpec3",None, gasUsePerCapitaDummyExtPrimary[:])
Data.addVarInt('gasPerCapitaDummyExtSpec2')
Data.store("gasPerCapitaDummyExtSpec2",None, gasUsePerCapitaDummyExtHigh[:])

	
end


//Generate normalized year values to reduce differences in magnitude of covariates.
//Scale year for better numerical performance
local yearScale = 1 
gen normYear = year/`yearScale'
gen quadYear = (year/1)^2

//Establish treatment interacted with pre-treatment population share in metro areas
frame copy default MetroShares
frame change MetroShares
keep if year==1970

cap gen preTreatMetroShare = metropopulation/(metropopulation+nonmetropopulation)

frame change default 

cap frlink m:1 state, frame(MetroShares)
cap frget preTreatMetroShare, from(MetroShares)

cap gen metroIntTreatment = nosafetyind*preTreatMetroShare

frame drop MetroShares
